package com.culturalCenter.dataCenter.Utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * 实体类对象工具类
 *
 * @author ISMC
 * @since 2020/10/20 14:50
 */
@Slf4j
public class ClassUtil {

    // 对象转map（对接 达实 参数处理）
    public static Map<String, Object> setConditionMap(Object obj) {
        Map<String, Object> map = new TreeMap<>();
        if (obj == null) {
            return null;
        }
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            Object value = (getValueByFieldName(field.getName(), obj));
            if (value != null) {
                map.put(field.getName(), value);
            }
        }
        map.put("timestamp", System.currentTimeMillis() / 1000);
        return map;
    }

    public static String setConditionString(Map<String, Object> map) {
        StringBuffer sb = new StringBuffer();
        if (map == null) {
            return "";
        }
        List<Map.Entry<String, Object>> list = new ArrayList<>(map.entrySet());
        // 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）：忽略大小写
        Collections.sort(list, (o1, o2) -> {
            String key1 = o1.getKey().toLowerCase();
            String key2 = o2.getKey().toLowerCase();
            int len1 = key1.length();
            int len2 = key2.length();
            int lim = Math.min(len1, len2);
            char v1[] = key1.toCharArray();
            char v2[] = key2.toCharArray();
            int k = 0;
            while (k < lim) {
                char c1 = v1[k];
                char c2 = v2[k];
                if (c1 != c2) {
                    return c1 - c2;
                }
                k++;
            }
            return len1 - len2;
        });

        for (Map.Entry<String, Object> entry : list) {
            if (entry.getValue() != null && !"".equals(entry.getValue())) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        return sb.toString();
    }

    public static Object getValueByFieldName(String fieldName, Object obj) {
        try {
            String fieldHead = fieldName.substring(0, 1).toUpperCase();
            String methodName = "get" + fieldHead + fieldName.substring(1);
            Method method = obj.getClass().getMethod(methodName, new Class[]{});
            return method.invoke(obj, new Object[]{});
        } catch (Exception e) {
            return null;
        }
    }

    public static String setParamString(Map<String, Object> param) {
        StringBuffer sb = new StringBuffer();
        if (param == null) {
            return "";
        } else {
            for (Map.Entry<String, Object> entry : param.entrySet()) {
                if (entry.getValue() != null && !"".equals(entry.getValue())) {
                    sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
            }
            return sb.toString().substring(0, sb.length());
        }
    }

}
